<template>
	<view class="content-img">
		<eui-nav-bar title="通讯录" color="#000" rightIcon="" />
		<eui-search-bar ref="approveSearch" v-model="searchValue" @confirm="onRefresh" radius="100"
			placeholder="搜索姓名、首字母">
		</eui-search-bar>
	</view>
	<view class="main-view font-size-28" :style="{width:scrollWidth,height:scrollHeight}">
		<view style="position: relative;border-radius: 10px;
		overflow: hidden;" :style="{width:scrollWidth,height:scrollHeight}">
			<ContactList :options="contactsList" @onRefresh="onRefresh" @scrolltolower="templateLower"
				@click="openDetail"></ContactList>
		</view>
	</view>
</template>

<script setup type="ts">
	import { ref } from 'vue';
	import { onLoad,onReady } from '@dcloudio/uni-app';
	import ContactList from '@/components/ContactList/index.vue'
	import service from '../../../common/service';
	import { useMainStore } from '../../../store/modules/store';
	import { useLoginStore } from '../../../store/modules/useLoginStore';
	const mainStore = useMainStore();
	const LoginStore = useLoginStore();
	
	const scrollWidth = ref(uni.getSystemInfoSync().windowWidth -20 + 'px');
	const scrollHeight = ref(uni.getSystemInfoSync().windowHeight + 'px');
	const CurrentPage = ref(1);//第几页
	const RowsCount = ref(0);//总条数
	const PageCount = ref(0);//总页数
	const PageSize = ref(40);//
	const searchValue = ref('');
	const contactsList = ref([])
	
	// 下拉刷新
	const onRefresh = ()=>{
		contactsList.value = [];
		CurrentPage.value = 1;
		GetUserInfoList();
	}
	// 加载更多
	const templateLower = ()=>{
		if (CurrentPage.value < PageCount.value) {
			CurrentPage.value = CurrentPage.value + 1;
			GetUserInfoList();
		}
	}
	const GetUserInfoList =()=>{
		service.enjoyShowLoading('正在加载...');
		let whereList = [];
		if(searchValue.value){
			whereList.push({
				c_type_name: '',
				c_col: 'p.c_name',
				c_oper: '相似',
				c_value: '%' + searchValue.value + '%',
				c_col_dtype: "System.String",
				c_logic: "或者",
				c_left: "(",
			});
			whereList.push({
				c_type_name: '',
				c_col: 'p.c_mobile_phone',
				c_oper: '=',
				c_value: searchValue.value,
				c_col_dtype: "System.String",
				c_logic: "或者",
				c_right: "",
			});
			whereList.push({
				c_type_name: '',
				c_col: 'p.c_phone',
				c_oper: '=',
				c_value: searchValue.value,
				c_col_dtype: "System.String",
				c_logic: "或者",
				c_right: "",
			});
			whereList.push({
				c_type_name: '',
				c_col: 'p.c_group',
				c_oper: '相似',
				c_value: searchValue.value + '%',
				c_col_dtype: "System.String",
				c_logic: "或者",
				c_right: "",
			});
			whereList.push({
				c_type_name: '',
				c_col: 'p.c_pycode',
				c_oper: '相似',
				c_value: searchValue.value + '%',
				c_col_dtype: "System.String",
				c_logic: "and",
				c_right: ")",
			});
		}
		let request = {
		    url: LoginStore.EnjoyMain,
		    data: {
		        UniqueKey: "管易通40通讯录",
		        ObjectData: {
					IsNotPage: false,
					CurrentPage: CurrentPage.value,
					PageSize: PageSize.value,
					WhereList: whereList,
					Tag: mainStore.loginInfo.sUser
				},//mainStore.loginInfo.sUser,
		        MethodName: "GetUserInfoList",
		    },
		}
		service.request(request, (res) => {
		    if (res && res.data) {
				if (!res.data.HasException) {
					const ObjectData = res.data.ObjectData;
					console.log("ObjectData: " + JSON.stringify(ObjectData));
					RowsCount.value = ObjectData.RowsCount;
					PageCount.value = ObjectData.PageCount;
					CurrentPage.value = ObjectData.CurrentPage;
					const imgUrl = LoginStore.EnjoyMain.replace('/Enjoy/Service', '');
					if (ObjectData.ReturnObject.length > 0) {
						for (let i = 0; i < ObjectData.ReturnObject.length; i++) {
							let sort = contactsList.value.length;
							let item = ObjectData.ReturnObject[i];
							if(item.c_name=='Group'){
								contactsList.value.push({
									letter:item.c_group.toUpperCase(),
									data:[],
								})
							}else{
								let hasGroup = 0;
								for (let bb = 0; bb < contactsList.value.length; bb++) {
									if(contactsList.value[bb].letter==item.c_group.toUpperCase()){
										contactsList.value[bb].data.push({
											...item,
											c_picurl:imgUrl+"/File/ShowUserImage/" + item.c_userno,
										})
										hasGroup+=1;
									}
								}
								if(hasGroup==0){
									let dd = [];
									dd.push({
										...item,
										c_picurl:imgUrl+"/File/ShowUserImage/" + item.c_userno,
									})
									contactsList.value.push({
										letter:item.c_group.toUpperCase(),
										data:dd,
									})
								}
							}
						}
					}
				} else {
					service.enjoyShowToast(res.data.Exception.Message)
				}
				service.enjoyHideLoading();
		    }
			service.enjoyHideLoading();
		}, () => {
			service.enjoyHideLoading();
		    console.log('失败')
		});
	}
	const openDetail = (item) =>{
		console.log("item: " + JSON.stringify(item));
		uni.navigateTo({
		  url: './contactDetail?userno='+item.item.c_userno
		});
	}
	onReady(()=>{
		uni.getSystemInfo({
			success: function(res) {
				const userInfoPage = uni.createSelectorQuery().select(".content-img");
				userInfoPage.boundingClientRect(function(data) {
					scrollHeight.value = (res.windowHeight - data.height - 10)+'px';
				}).exec()
			}
		});
	})
	onLoad(()=>{
		GetUserInfoList();
	})
</script>

<style lang="scss" scoped>
	@import '@/main.scss';

	.content-img {
		background-color: #FFFFFF;
		background-repeat: no-repeat;
		width: 100%;
	}

	.main-view {
		flex: 1;
		flex-direction: column;
		background-color: #f2f2f2;
		padding: 10px 10px 0px 10px;
	}

	.flex {
		display: flex;
	}

	.font-size-28 {
		font-size: 28rpx;
	}
</style>